//
//  StatusCell.swift
//  SinaWeibo
//
//  Created by Tony-ztb on 15/12/19.
//  Copyright © 2015年 Tony-ztb. All rights reserved.
//

import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    // 定义底部视图约束的属性
    var bottomViewConstrants: Constraint?

    // 定义微博模型属性
    var status: Status? {
        didSet {
            // 顶部视图 设置数据
            topView.status = status
            // 转发视图 的模型属性
            // 先解除约束
            self.bottomViewConstrants?.uninstall()
            // 根据提供的数据 判断是否显示转发视图
            if let retweetedStatus = status?.retweeted_status {
                // 转发微博
                // 修改 底部视图 约束
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewConstrants = make.top.equalTo(retweetView.snp_bottom).constraint
                    // 设置转发微博的数据
                    retweetView.retweetedStatus = retweetedStatus
                    // 显示转发微博
                    retweetView.hidden = false
                })
                
            } else {
                // 是原创微博,不是转发微博
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    self.bottomViewConstrants = make.top.equalTo(topView.snp_bottom).constraint
                    // 显示转发微博
                    retweetView.hidden = true
                })
                
            }
            
            
        }
    }
    
    // cell 入口
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        // 设置cell选中的样式
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    // 自定义cell
    private func setupUI() {
        // 添加子视图
        contentView.addSubview(topView)
        contentView.addSubview(retweetView)
        contentView.addSubview(bottomView)
        
        // 给子视图添加约束
        // topView添加约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(contentView)
            // 设置高度 ,便于测试
//            make.height.equalTo(50)
        }
        
        // retweetView添加约束
        retweetView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(topView.snp_bottom)
        }
        
        // bottomView添加约束
        bottomView.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(self)
            self.bottomViewConstrants = make.top.equalTo(retweetView.snp_bottom).constraint
            make.height.equalTo(40)
        }
        
        // 给contentView设置约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.left.right.equalTo(self)
            make.bottom.equalTo(bottomView.snp_bottom)
        }
        
    }
    
    // 懒加载子视图
    // 顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    // 转发视图
    private lazy var retweetView: StatusRetweetView = StatusRetweetView()
    // 底部视图
    private lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
}
